#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#
#
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Decode_Month Add_Delta_YM Add_Delta_Days Today);
use Text::CSV;

use avsCommon;

dbsetup();

my ($content, $response, $request, $url);
my ($period, $custid);
my ($runyear, $runmonth, $runday);

# create user agent, set cookies
my $ua = new LWP::UserAgent(keep_alive=>1);
$ua->timeout(300);
$ua->cookie_jar( {} );

# loop through all the colleges
do {
	getLogin ();
	
	($runyear, $runmonth, $runday) = @enddate;
	
	# run once for each year as that is the most we can ask for in a report. typicall only run for one month.
	while ($runyear > $begyear || $runyear == $begyear && $runmonth >= $begmonth) {
		getStats ();
		$runyear--;
	}

	logout ();
	
} while $sthlogin->fetch;

dbshutdown();

exit();

sub getLogin {
	# login to get cookies setup cookies
	$url = 'http://www.columbiaonlineadmin.org/ams4/login/auth.cgi';
	$request = HTTP::Request->new(POST => $url);
	$request->content_type('application/x-www-form-urlencoded');
	$request->content("func=login&url=&username=$username&password=$password");

	$response = $ua->request($request);
	if (!$response->is_success && substr($response->status_line,0,3) ne '302') {
		die "#error:$0: POST report failed $url : " . $response->status_line;
	}
}

sub getStats {
	my ($runendyear, $runendmonth, $runendday) = 	Add_Delta_Days(Add_Delta_YM($runyear, $runmonth, 1, 0, 1), -1); # find last day of month by going to first day of next month and moving back one day
	my ($runbegyear, $runbegmonth, $runbegday) = 	Add_Delta_YM($runyear, $runmonth, 1, 0, -11); # go back to begining of 12th month

	# don't got beyond begin date
	if ($runbegyear < $begyear || $runbegyear == $begyear && $runbegmonth < $begmonth) {
		$runbegyear = $begyear;
		$runbegmonth = $begmonth;
	};
	
	#ask for the report
	$url = 'http://www.columbiaonlineadmin.org/ams4/subscriber/reporting/SearchSessionReport.php?export=1&fromDate=' . 
		$runbegmonth . '-1-' . $runbegyear . '&toDate=' . $runendmonth . '-' . $runendday . '-' . "$runendyear&id=$option1";
		
	$response = $ua->request(GET $url);
	if (substr($response->status_line,0,3) eq '500') {
		print "#info: timeout retry\n";
		sleep 5;
		$response = $ua->request(GET $url);
	};
	if (!$response->is_success) {
		die "#error:$0: GET MENU $url : " . $response->status_line;
	};

	$content = $response->decoded_content;

	# save contents for possible debugging
	savecontent('Columbia', "$collegeid", $content);

	$content =~ /<table.*?<table.*?\/tr>(?<data>.*?)<\/table/sm;

	$content = $+{data};
	
	# we now have the report in html format.
	# take the html and reformat for easier data extraction
	$content =~ s/\s\s//g; # remove spaces/cr lf
	$content =~ s/<\/tr>/\n/g; # add cr
	$content =~ s/<\/td>/~/g; # add splitable character
	$content =~ s/<.*?>//g; # remove html
	$content =~ s/ ~/~/g; # remove extra space in datasources

	# find the actual data table
	my @contents = split("\n",$content);

	# grab the dates off of the header lines.
	my @periods = ('','','','');	
	my $begperiod = myperiod($begyear, $begmonth);
	my $endperiod = myperiod($runyear, $runmonth);
	my $begperiodndx = 4;
	my $endperiodndx = 4;

	while ($contents[3] =~ /(?<month>\w+)-(?<year>\d{4})/g) {
		my $period = myperiod($+{year},$+{month});
		$begperiodndx++ if ($period lt $begperiod);
		$endperiodndx++ if ($period lt $endperiod);
		push @periods, $period;
	}
	$begperiodndx-- if $begperiodndx == 16;	# prevent multiyear problem
	$endperiodndx-- if $endperiodndx == 16;	# prevent multiyear problem

	# go through each line of data
	foreach my $contents (@contents[4..$#contents]) {

		# ignore blank lines
		next if ($contents eq '~'||$contents eq ''); 

		#split the data
		my @columns = split('~',$contents);

		# update information for each datasource
		if ($columns[0] ne '') {
			# determine data type and save in correct month
			my $type = ($columns[3] eq 'Sessions') ? '1' : '2';

			foreach my $i ($begperiodndx..$endperiodndx) {
				print "$vendorid $collegeid $periods[$i] '$columns[0]' $columns[$i] $type\n";
				$sthstats->execute($vendorid, $collegeid, $columns[0], $periods[$i], $columns[$i], $type);
			}
		}
	}
}

sub logout {
	$url = "http://www.columbiaonlineadmin.org/ams4/login/auth.cgi?func=logout";
	$response = $ua->request(GET $url);
}